//
//  OAAddresssecondViewController.m
//  tongxiangOASystem
//
//  Created by michael on 2019/2/22.
//  Copyright © 2019年 czd. All rights reserved.
//

#import "OAAddresssecondViewController.h"
#import "OANameListController.h"
#import "OAMainAddressViewController.h"
#import "OAExpendViewController.h"
#import "OACountyTableViewController.h"
#import "OAAddresssController.h"
#import "OAaddresslistViewController.h"
#import "OAAddressViewController.h"
#import "HttpTools.h"
#import "OADepAddressController.h"
#import "MBProgressHUD.h"


@interface AddressCellsecond : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imageVIew;
@property (weak, nonatomic) IBOutlet UILabel *subject;
@end
@implementation AddressCellsecond

@end
@interface OAAddresssecondViewController ()
<UIAlertViewDelegate>
@property(nonatomic,strong)NSMutableArray* contents;
@property(strong,nonatomic)NSMutableArray * searchResult;
@end

@implementation OAAddresssecondViewController

-(NSMutableArray*)contents{
    if (_contents == nil) {
        _contents = [NSMutableArray array];
    }
    
    return _contents;
}

-(NSMutableArray*)searchResult{
    if (_searchResult == nil) {
        _searchResult = [NSMutableArray array];
    }
    
    return _searchResult;
}
- (void)viewDidLoad {
if ([_type isEqualToString:@"renyuan"]) {
    self.title = @"人员查询";
[self setRightItem];
}
    [super viewDidLoad];
    [self requestData];
//    [self setRightItem];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)setRightItem{
    UIBarButtonItem * rightitem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    self.navigationItem.rightBarButtonItem = rightitem;
}

-(void)searchAction{

    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"搜索通讯录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.delegate = self;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * textField = [alertView textFieldAtIndex:0];
    textField.placeholder = @"请输入关键字";
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {
        UITextField * textField = [alertView textFieldAtIndex:0];
        if (textField.text.length == 0) {
            return;
        }
        [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        NSArray *  array = @[@{@"condition":textField.text},@{@"userId":userId},@{@"page":@"1"}];;
        [HttpTools requestWithURL:KExchother arrays:array method:@"adressListSearch" success:^(id JSON) {
       
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
            NSArray * tempArray = JSON[@"data"];
            
            if (tempArray.count) {
    

            NSDictionary *dict = [tempArray firstObject];
           [self.searchResult removeAllObjects];
            if (dict) {
//                [self.searchResult addObject:dict];
                self.searchResult = [tempArray mutableCopy];
            }
          if (JSON) {
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            OADepAddressController * depVC = (OADepAddressController*)[storyBoard instantiateViewControllerWithIdentifier:@"DepAddressID" ];
            depVC.contents = self.searchResult;
            depVC.hidesBottomBarWhenPushed = YES;

            [self.navigationController pushViewController:depVC animated:YES];
}
            
}else{
NSLog(@"11111");
}
            
        } failure:^(NSError *error) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        }];
    }
    
    
}
#pragma mark - Table view data source
-(void)requestData{
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray *  array = @[@{@"path":_path}];
 if ([_type isEqualToString:@"renyuan"]) {
_method = @"SecTree";
_url = @"exchTree";

}else{
_method = @"SecTree";
_url = @"exchTree";

}
    [HttpTools requestWithURL:_url arrays:array method:_method success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSLog(@"%@",JSON);
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
        self.contents = dict[@"Parent"];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressCellsecond * cell = [tableView dequeueReusableCellWithIdentifier:@"AddressCellsecond" forIndexPath:indexPath];
    NSDictionary * dict = self.contents[indexPath.row];
    cell.subject.text = dict[@"bmmc"];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contents.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([_type isEqualToString:@"renyuan"]) {
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OAaddresslistViewController *depVC = [[OAaddresslistViewController alloc]init];
//    OADepAddressController * depVC = (OADepAddressController*)[storyBoard instantiateViewControllerWithIdentifier:@"DepAddressID" ];
     NSDictionary * dict = self.contents[indexPath.row];
    depVC.path = dict[@"path"];
                depVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:depVC animated:YES];
}else{
UIStoryboard * stroyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

//     CountyListModel * listModel = self.contentsArray[indexPath.row];
     OANameListController * expendVC = (OANameListController*)[stroyBoard instantiateViewControllerWithIdentifier:@"NameLIstID"];

     NSDictionary * dict = self.contents[indexPath.row];
    expendVC.identifierString = dict[@"path"];
                         expendVC.hidesBottomBarWhenPushed = YES;
expendVC.type = @"CountyDataType";
    [self.navigationController pushViewController:expendVC animated:YES];
}
   

}

@end
